# -*- coding: utf-8 -*-
# @Time    : 2021/8/30 15:12
# @Author  : BrandonGT
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class MyWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.resize(1024, 768)
        self.setWindowOpacity(0.8)
        self.button_size_x = 80
        self.button_size_y = 40

        self.setup_ui()

    def setup_ui(self):

        self.close_btn = QPushButton('关闭')
        self.close_btn.resize(self.button_size_x, self.button_size_y)
        self.close_btn.setParent(self)
        self.close_btn.clicked.connect(self.close)

        self.max_btn = QPushButton('最大化')
        self.max_btn.resize(self.button_size_x, self.button_size_y)
        self.max_btn.setParent(self)
        self.max_btn.clicked.connect(self.min_max)

        self.min_btn = QPushButton('最小化')
        self.min_btn.resize(self.button_size_x, self.button_size_y)
        self.min_btn.setParent(self)
        self.min_btn.clicked.connect(self.showMinimized)

    def resizeEvent(self, QResizeEvent):
        window_width = int(self.width())
        close_move = window_width - self.button_size_x
        max_move = window_width - self.button_size_x * 2
        min_move = window_width - self.button_size_x * 3
        self.close_btn.move(close_move, 0)
        self.max_btn.move(max_move, 0)
        self.min_btn.move(min_move, 0)

    def mouseMoveEvent(self, QMouseEvent):
        move_x = self.hit_x - QMouseEvent.globalX()
        move_y = self.hit_y - QMouseEvent.globalY()
        final_x = self.origin_x - move_x
        final_y = self.origin_y - move_y
        self.move(final_x, final_y)

    def mousePressEvent(self, QMouseEvent):
        self.setMouseTracking(True)
        self.hit_x = QMouseEvent.globalX()
        self.hit_y = QMouseEvent.globalY()
        self.origin_x = self.x()
        self.origin_y = self.y()

    def mouseReleaseEvent(self, QMouseEvent):
        self.setMouseTracking(False)

    def min_max(self):
        if not self.isMaximized():
            self.max_btn.setText("恢复")
            self.showMaximized()
        else:
            self.showNormal()
            self.max_btn.setText("最大化")

app = QApplication(sys.argv)
main_window = MyWindow()
main_window.show()
sys.exit(app.exec_())